<template>
	<view class="home">
		<navbar :isSearch="true" v-model="value" @input="change"></navbar>
		<view class="home-list">
			<view v-if="is_history" class="label-box">
				<view class="label-header">
					<text class="label-title">搜索历史</text>
					<text class="label-clear" @click="clear">清空</text>
				</view>
				<view v-if="historyLists.length > 0" class="label-content">
					<view class="label-content__item" v-for="(item,index) in historyLists" :key="index"
						@click="openHistory(item)">{{item.name}}</view>
				</view>
				<view v-else class="no-data">
					没有搜索历史
				</view>
			</view>
			<list-scroll v-else class="list-scroll">
				<uni-load-more v-if="loading" status="loading" iconType="snow"></uni-load-more>
				<view v-if="searchList.length > 0">
					<!-- <list-card :item="item" v-for="item in searchList" :key="item.id" @click="setHistory"></list-card> -->

					<view @click="setHistory">
						<list-block :list="searchList"></list-block>
					</view>


				</view>

				<view class="more" v-if="showButtonLoading">
					<uni-load-more :status="more" :content-text="contentText"></uni-load-more>
				</view>


			</list-scroll>
		</view>
	</view>
</template>

<script>
	import {
		mapState
	} from 'vuex'
	
	export default {
		data() {
			return {
				value: '',
				is_history: true,
				searchList: [],
				loading: false,
				more: 'loading',
				showButtonLoading: false,
				currentPage: 1,
				timer: null,
				contentText: {
					contentdown: '查看更多',
					contentrefresh: '加载中',
					contentnomore: '没有更多'
				}
			}
		},
		computed: {
			...mapState(['historyLists'])
		},
		onLoad() {
			


		},
		onShow() {
			// 微信分享
			this.shareWx('','大地搜索')
		},
		// 上滑加载
		onReachBottom() {
			// this.more == moMore 时候表示已经加载完成,不需要再请求了
			if (this.more == 'moMore') {
				return;
			} else {
				this.getSearch(this.value, 2);
			}
		},
		methods: {
			//清空历史记录
			clear() {
				this.$store.dispatch('clear_history');
				uni.showToast({
					title: '清空完成'
				})
			},
			// 设置历史记录
			setHistory(value) {
				let searcharr = [];
				
				for (let i = 0; i < this.historyLists.length; i++) {
					searcharr.push(this.historyLists[i].name);
				}
				
				if(!searcharr.includes(value)){
					this.$store.dispatch('set_history', {
						name: this.value
					})
				}

			},
			openHistory(item) {
				this.value = item.name
				this.getSearch(this.value)
			},
			change(value) {
				if (!value) {
					clearTimeout(this.timer)
					this.mark = false;
					this.getSearch(value)
					return
				}
				// 做标记
				if (!this.mark) {
					this.mark = true;
					this.timer = setTimeout(() => {
						this.mark = false;
						this.getSearch(this.value);

					}, 1000)
				}
			},
			getSearch(value) {
				if (!value) {
					this.searchList = [];
					this.is_history = true;
					return;
				}
				this.is_history = false

				var _this = this


				_this.$api.get_search({
					search: value
				}).then(res => {

					if (res.code === 1) {
						console.log(res.data)
						if (res.data.length === 0) {
							//加载完成
							this.more = 'moMore'
							this.showButtonLoading = true
						}

						_this.searchList = _this.searchList.concat(res.data)
						_this.currentPage = _this.currentPage + 1;

						// _this.setHistory(value)

						//隐藏加载
						uni.hideLoading()


					} else {
						//提示刷新成功
						_this.$refs.popuperror.open()
						//隐藏加载
						uni.hideLoading()
					}



				}).catch(() => {

				})
			}
		}
	}
</script>

<style lang="scss">
	page {
		height: 100%;
		display: flex;
		background-color: #f5f5f5;
	}

	.home {
		display: flex;
		flex-direction: column;
		flex: 1;
		overflow-y: auto;

		.label-box {
			background-color: #fff;
			margin-bottom: 10px;

			.label-header {
				display: flex;
				justify-content: space-between;
				font-size: 14px;
				color: #666;
				padding: 10px 15px;
				border-bottom: 1px #f5f5f5 solid;

				.label-title {
					color: $mk-base-color;
				}

				.label-clear {
					color: #30b33a;
					font-weight: bold;
				}
			}

			.label-content {
				display: flex;
				flex-wrap: wrap;
				padding: 15px;
				padding-top: 0;

				.label-content__item {
					padding: 2px 10px;
					margin-top: 12px;
					margin-right: 10px;
					border-radius: 5px;
					border: 1px #666 solid;
					font-size: 14px;
					color: #666;
				}
			}


		}

		.no-data {
			height: 200px;
			line-height: 200px;
			width: 100%;
			text-align: center;
			color: #666;
			font-size: 12px;
		}

		.more {
			padding: 20rpx 0;
		}
	}
</style>
